import sys
from PyQt5.QtWidgets import QWidget,QApplication
from PyQt5.QtCore import QThread, pyqtSignal,QObject
from time import sleep
import ui_test
import threading

class test_Qthread_function(QObject):

    signal_start_function = pyqtSignal()
   
    def __init__(self,parent=None):
        super(test_Qthread_function,self).__init__(parent)
        #开始调用网络的信号
    
    def qthread_function1(self):
        sleep(1)
        print("运行线程id:",threading.current_thread().ident)

class InitForm(QWidget):
    def __init__(self):
        super().__init__()
        self.ui = ui_test.Ui_Form()
        self.ui.setupUi(self)
        self.setWindowTitle("测试")

        print("主线程id:",threading.current_thread().ident)

        self.test1_QThread  = QThread()
        self.test1_Qthread_function = test_Qthread_function()
        self.test1_Qthread_function.moveToThread(self.test1_QThread)    
        self.test1_QThread.start()  

        self.test1_Qthread_function.signal_start_function.connect(self.test1_Qthread_function.qthread_function1)
        self.test1_Qthread_function.signal_start_function.emit() #运行第一次
        self.test1_Qthread_function.signal_start_function.emit() #运行第二次
        
    def closeEvent(self, event):
        print("窗体关闭")
        self.test1_QThread.quit()      #回收线程
        self.test1_QThread.wait()      #回收线程
        del self.test1_Qthread_function

if __name__ == "__main__":
    app = QApplication(sys.argv)
    w1 = InitForm()
    w1.show()
    sys.exit(app.exec_())